// Copyright (c) 2023 Presto Labs Pte. Ltd.
// Author: daniel

#include "coin2/exchange/base/order/recent_active_symbol.h"

#include <glog/logging.h>

namespace coin2::exchange::base::order {

RecentActiveSymbol::RecentActiveSymbol(int64_t window_size) : active_symbol_window_(this) {
  CHECK(window_size > 0);
  active_symbol_window_.ResizeWindow(window_size);
}

void RecentActiveSymbol::AddActiveEvent(int64_t ts, const std::string& symbol) {
  if (ts <= 0) {
    return;
  }
  active_symbol_window_.Push(ts, symbol);
}

bool RecentActiveSymbol::HasActiveEvent(int64_t ts, const std::string& symbol) {
  if (ts <= 0) {
    return false;
  }
  active_symbol_window_.PushTime(ts);
  return active_symbol_map_.count(symbol) > 0 && active_symbol_map_[symbol] > 0;
}

void RecentActiveSymbol::AddEvent(int64_t ts, const std::string& symbol) {
  if (active_symbol_map_.count(symbol) == 0) {
    active_symbol_map_[symbol] = 1;
  } else {
    ++active_symbol_map_[symbol];
  }
}

void RecentActiveSymbol::DelEvent(int64_t ts, const std::string& symbol) {
  CHECK(active_symbol_map_[symbol] > 0)
      << "abnormal symbol: " << symbol << " - " << active_symbol_map_[symbol];
  if (--active_symbol_map_[symbol] == 0) {
    active_symbol_map_.erase(symbol);
  }
}

}  // namespace coin2::exchange::base::order
